/*
 * ComerciosAgregarFrame.java
 *
 * Created on 16 de abril de 2008, 12:06 PM
 */
package org.contabilidad.codigosvigencias;


import org.mybeans.Sesion;
 
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.text.Format;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author  avbravo
 */
public class CodigosVigenciasEliminarFrame extends javax.swing.JFrame {

    Sesion sesion;

    /** Creates new form ComerciosAgregarFrame */
    public CodigosVigenciasEliminarFrame() {
        initComponents();
        sesion = new Sesion();

        /*
         * Fondo
         */

         
         
         

        jPanel3.setOpaque(false);
        setContentPane(jPanel3);
        /*
         * fondo
         */


        Consultar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldIdCodigoVigencia = new javax.swing.JTextField();
        jButtonRegresar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Eliminación de Codigos de Vigencias");

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Codigo");

        jTextFieldIdCodigoVigencia.setColumns(35);
        jTextFieldIdCodigoVigencia.setEditable(false);
        jTextFieldIdCodigoVigencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdCodigoVigenciaActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(29, 29, 29)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextFieldIdCodigoVigencia, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIdCodigoVigencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldIdCodigoVigenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdCodigoVigenciaActionPerformed
}//GEN-LAST:event_jTextFieldIdCodigoVigenciaActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
    // TODO add your handling code here:
      try {
        String IdRegimenVigencia = this.jTextFieldIdCodigoVigencia.getText();
        if (IdRegimenVigencia == null || IdRegimenVigencia.equals("")) {
            JOptionPane.showMessageDialog(this, "No existe un codigo de vigencia ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdCodigoVigencia.requestFocus();
            return;
        }

        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String sqlquery = "select * from codigosvigencias where idmunicipio=? and activo='si' ";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();

        if (x == 0) {
            JOptionPane.showMessageDialog(this, "No existe un codigo de vigencia", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }


        int res = JOptionPane.showConfirmDialog(this, "Desea eliminar este codigo vigencias",
                "Eliminar Codigo Vigencias ", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {

            String sql = "delete from codigosvigencias where idmunicipio=? and idregimenvigencia= ? and activo ='si' ";
            PreparedStatement prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, IdRegimenVigencia);

            prest.executeUpdate();
            sesion.con.close();
            
            JOptionPane.showMessageDialog(this, "Codigos de vigencia eliminadoexitosamente", "Eliminado", JOptionPane.INFORMATION_MESSAGE);
            Limpiar();
        } else {
            JOptionPane.showMessageDialog(this, "No se elimino el codigo de vigencia.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (Exception ex) {
        System.out.println("Error" + ex);
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CodigosVigenciasEliminarFrame().setVisible(true);
            }
        });
    }

    private void Limpiar() {
        this.jTextFieldIdCodigoVigencia.setText("");

        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
    
    }

    private void Consultar() {
        try {



            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select *  from codigosvigencias where idmunicipio = ? and activo='si'";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(this, "No se ha creado el codigo de vigencias ", "Error", JOptionPane.INFORMATION_MESSAGE);
            } else {
                String IdRegimenVigencia = rs.getString("idregimenvigencia");

                jTextFieldIdCodigoVigencia.setText(IdRegimenVigencia);


                Format formatter;
                formatter = new SimpleDateFormat("dd/MM/yyyy");
                //Date fecha = rs.getDate("fechainicio");
               // String MyFecha = formatter.format(fecha);

                repaint();

            }



        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextFieldIdCodigoVigencia;
    // End of variables declaration//GEN-END:variables
}
